package com.sy.core.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.core.model.SyArticle;
import com.sy.core.model.SyProductType;
import com.sy.core.util.RestResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品分类模块
 */
@Controller
@RequestMapping("article")
public class ArticleCtl extends BaseCtl{

    @RequestMapping("index")
    public String index(){
        return "back/article/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public RestResult list(Integer page,Integer limit){
        RestResult rr = null;
        Map<String,Object> map = new HashMap<>();
        try {
            PageHelper.startPage(page,limit);
            List<Map<String, Object>> mapList = articleService.queryList(null);
            PageInfo<Map<String, Object>> info = new PageInfo<>(mapList);
            rr= new RestResult("200","获取文章列表信息失败",info);
        } catch (Exception e) {
            logger.error("获取文章列表信息失败");
            rr=new RestResult("201","获取文章列表信息失败");
        }
        return rr;
    }

    @RequestMapping("add")
    public String add(){
        return "back/article/articleEdit";
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public RestResult saveOrUpdate(SyArticle syArticle){
        RestResult rr = null;
        try {
            String pk = articleService.save(syArticle);
            rr = new RestResult("200","保存数据成功",pk);
        } catch (Exception e) {
            logger.error("保存数据失败");
            rr = new RestResult("201","保存数据失败");
        }
        return rr;
    }

    @RequestMapping("update")
    public String update(ModelMap mv,String pk){
        Map<String, Object> map = articleService.queryOne(pk);
        mv.addAttribute("mv",map);
        return "back/article/articleEdit";
    }


    @RequestMapping("del")
    @ResponseBody
    public RestResult del(String pks){
        RestResult rr = null;
        try {
            articleService.batchDel(pks);
            rr=new RestResult("200","删除成功");
        } catch (Exception e) {
            logger.error("删除失败");
            rr=new RestResult("201","删除失败");
        }
        return rr;
    }

}
